package com.irdstudio.efp.nls.service.impl.yed.queue;

import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.framework.core.threadpool.ApplicationThreadPool2;
import com.irdstudio.basic.framework.core.util.SpringContextUtils;
import com.irdstudio.efp.basic.framework.mq.handle.MQMessageProcessor;
import com.irdstudio.efp.loan.service.vo.AccLoanVO;
import com.irdstudio.efp.nls.service.vo.NlsApplyInfoVO;
import org.apache.rocketmq.common.message.MessageExt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.io.UnsupportedEncodingException;
import java.util.concurrent.TimeUnit;

import static com.irdstudio.efp.basic.framework.mq.constant.TopicEnums.YED_SETTLE_CERT_QUEUE_TASK_TOPIC;

/**
 * 优e贷结清文件申请队列服务
 *
 * @author liangcongming :2020/8/11 18:31
 * @version 0.1
 * @date 2020/8/11 18:31
 * @since 2020/8/11 18:31
 */
public class YedSettleCertQueueTaskMessageProcessorImpl implements MQMessageProcessor {

    @Autowired
    @Qualifier("applicationThreadPool")
    private ApplicationThreadPool2 threadpool;

    private StringRedisTemplate template;

    public void setTemplate(StringRedisTemplate template) {
        this.template = template;
    }

    private static Logger logger = LoggerFactory.getLogger(YedSettleCertQueueTaskMessageProcessorImpl.class);

    private static final String prefix = "rc:" + YED_SETTLE_CERT_QUEUE_TASK_TOPIC.getTopicId() + ":";

    public boolean handleMessage(MessageExt messageExt) {
        try {
            if (isRepeat(messageExt)) {
                logger.warn("消息重复发送！messageid: " + messageExt.getMsgId()
                        + " key: " + messageExt.getKeys());
            } else {
                String topic = messageExt.getTopic();
                String tag = messageExt.getTags();
                String key = messageExt.getKeys();
                String body = new String(messageExt.getBody(), "UTF-8");
                logger.info("获取消息成功，消息主题Topic：【" + topic + "】,消息标签tag：【" + tag + "】,消息Key:【" + key + "】,消息体：【" + body + "】");

                AccLoanVO accLoanVO = JSONObject.parseObject(body, AccLoanVO.class);
                YedSettleCertQueueHandle executoryQueueThread = (YedSettleCertQueueHandle) SpringContextUtils.getBean("yedSettleCertQueueHandleService");
                executoryQueueThread.setAccLoanVO(accLoanVO);
                try {
                    threadpool.add(executoryQueueThread);
                } catch (Exception e) {
                    logger.error(e.getMessage());
                }
            }

        } catch (UnsupportedEncodingException e) {
            logger.error(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return true;
    }

    private boolean isRepeat(MessageExt messageExt) {
        String key = messageExt.getKeys();
        String checkKey = prefix + key;
        if (template.opsForValue().setIfAbsent(checkKey, "")) {
            template.expire(checkKey, 12, TimeUnit.HOURS);
            return false;
        }
        return true;
    }

}
